//
//  main.m
//  OC05 _ 数组_字典_集合
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contect.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {

    
    
# pragma mark - 知识点1 数组
        
# pragma mark * 不可变数组
        
# pragma mark ** 创建对象 API
#if 0
        /* init方法 */
        NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"dalian",@"haerbin", nil];
        /* 构造器方法 */
        NSArray *array2 = [NSArray arrayWithObjects:@"beijing",@"dalian",@"haerbin", nil];
        
        /*字面量*/
        NSArray *array3 = @[@"beijing",@"dalian",@"haerbin"];
        

        
#pragma mark ** 获取数组元素个数 API
        
        NSArray *array = @[@"beijing",@"dalian",@"haerbin"];
        NSLog(@"%ld",array.count);

        
# pragma mark ** 获取数组中指定下标对应的元素 API
        NSArray *array = @[@"beijing",@"dalian",@"haerbin"];
        
        [array objectAtIndex:1];
        /* 注意  从数组中获取对象是  一定要明确对象类型 */
        NSString *city = [array objectAtIndex:1];
        NSLog(@"city:%@",city);
        /* 字面量方式获取对象 */
        NSString *name = array [0];
        NSLog(@"name:%@",name);
   
        
# pragma mark ** 用来判断数组中是否包含某一个给定的对象 API
        
        NSArray *array = @[@"beijing",@"dalian",@"haerbin"];
        [array containsObject:@"dalian"];
        if ([array containsObject:@"dalian"]) {
            NSLog(@"含有大连");
        }

        
        
# pragma mark ** 获取数组元素对应的索引 API
        
        NSArray *array = @[@"beijing",@"dalian",@"haerbin"];
        [array indexOfObject:@"beijing"];
        NSLog(@"%ld",[array indexOfObject:@"beijing"]);

        
       
# pragma mark ** 按照给定的字符串进行截取，将截取的多段字符串放入数组中 API
        
        NSString *str = @"www.baidu.com";
        /* 注意  这是字符串方法  返回值是数组 */
        NSArray *arr = [str componentsSeparatedByString:@"."];
        NSLog(@"arr:%@",arr);

# pragma mark ** 将数组中的元素按照给定的字符串格式拼接成一个完整的字符串对象 API
    
        NSArray *arr = @[@"dalian",@"beijing",@"harebin"];
        /* 注意  这是数组的方法, 返回值是字符串 */
        NSString *str=[arr componentsJoinedByString:@"&"];
        NSLog(@"str:%@",str);

        
        /*练习   存在如下的字符串，将其中的图片的网址提取出来。
         http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg*/
        
        NSString *str = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
        NSArray *arr = [str componentsSeparatedByString:@"&"];
        NSLog(@"arr:%@",arr);
        /* 有如下一个数组，@[@“type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345”]，将其中的内容用”&"符号拼接成一个字符串 */
        NSArray *arr1 = @[@"type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345"];
        NSString *str1 = [arr1 componentsJoinedByString:@"&"];
        NSLog(@"str:%@", str1);
#endif
        
        
        
#pragma mark - * 可变数组
        
        
# pragma mark ** 数组中添加一个对象
#if 0
      
        NSMutableArray *mArr = [[NSMutableArray alloc]initWithObjects:@"dalian",@"beijing",@"haerbin", nil];
        
        NSMutableArray *mArr2 = [NSMutableArray arrayWithCapacity:20];
        /* 字面量方式  后面加上mutableCopy  copy一份可变的数组 */
        NSMutableArray *mArr3 = @[@"beijing",@"dalian",@"haerbin"].mutableCopy;
        
        
        

# pragma mark ** 数组中指定位置插入一个对象
        NSMutableArray *mArr = @[@"dalian",@"beijing",@"haerbin"].mutableCopy;
        /* 注意  添加对象在数组最后 */
        [mArr addObject:@"shenyang"];
        NSLog(@"mArr:%@",mArr);

        NSMutableArray *mArr = @[@"beijing",@"dalian",@"haerbin"].mutableCopy;
        [mArr insertObject:@"shenyang" atIndex:1];
        
        NSLog(@"%@",mArr);

        
# pragma mark ** 数组中移除一个对象 API
        
        NSMutableArray *mArr = @[@"beijing",@"dalian",@"haerbin"].mutableCopy;
        [mArr removeObject:@"dalian"];
        NSLog(@"%@",mArr);
        
        NSMutableArray *mArr1 = @[@"beijing",@"dalian",@"haerbin"].mutableCopy;
        [mArr1 removeLastObject];
        NSLog(@"%@",mArr1);

        
# pragma mark ** 移除数组中最后一个对象 API
        Last
# pragma mark ** 移除数组中所有的元素 API
        All
# pragma mark ** 数组中移除指定位置的元素 API
        AtIndex

# pragma mark ** 使用指定的对象替换指定位置的对象 API
        
        NSMutableArray *mArr = @[@"bejing",@"haerbin",@"dalian"].mutableCopy;
        [mArr replaceObjectAtIndex:1 withObject:@"America"];
        NSLog(@"%@",mArr);

# pragma mark ** 交换指定的两个下标对应的对象 API
        NSMutableArray *mArr = @[@"bejing",@"haerbin",@"dalian"].mutableCopy;
        [mArr exchangeObjectAtIndex:1 withObjectAtIndex:2];
        NSLog(@"%@",mArr);
#endif
        
# pragma mark - 知识点2 字典
# pragma mark - * 不可变字典
# pragma mark ** 创建对象
#if 0
        /*int方法
         * 注意  Key和Value的顺序.
         */
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        /*构造器方法*/
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex",nil];
        /*字面量方式*/
        /*注意顺序  key : value , ......*/
        NSDictionary *dic3 = @{@"name":@"zhangsan",
                               @"age":@"20",
                               @"sex":@"male"};

        
        
# pragma mark ** 获取字典中键值对的个数 API
      
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex" ,nil];
        NSLog(@"%ld",dic.count);

        
# pragma mark ** 获取字典中所有的键 API
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        [dic allKeys];
        NSLog(@"%@",dic.allKeys);

# pragma mark ** 获取字典中所有的值 API
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        [dic allValues];
        NSLog(@"%@",dic.allValues);

# pragma mark ** 根据键获得对应的值 API
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        
        NSString *name = [dic objectForKey:@"name"];
        NSString *age = [dic objectForKey:@"age"];
        NSString *sex = [dic objectForKey:@"sex"];
#endif
        
# pragma mark - * 可变字典
# pragma mark ** 创建对象
#if 0
        NSMutableDictionary *mdic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        
        NSMutableDictionary *mdic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20",@"age",@"male",@"sex", nil];
        NSMutableDictionary *mdic3 = @{@"name":@"zhangsan",
                                       @"age":@"20",
                                       @"sex":@"male"}.mutableCopy;
        /*创建空的可变字典*/
        NSMutableDictionary *mdic4 = [NSMutableDictionary dictionary];
        NSMutableDictionary *mdic5 = [NSMutableDictionary dictionaryWithCapacity:26];
        

        
# pragma mark ** 字典中添加新的键值对以及 修改某一对键值对 API
        
        NSMutableDictionary *mdic = @{@"name":@"zhangsan",
                                      @"age":@"20",
                                      @"sex":@"male"}.mutableCopy;
        [mdic setObject:@"fuck" forKey:@"hobby"];
        [mdic setObject:@"20" forKey:@"age"];
        NSLog(@"%@",mdic);

        
# pragma mark ** 移除指定的键对应的键值对 API
        NSMutableDictionary *mdic = @{@"name":@"zhangsan",
                                      @"age":@"20",
                                      @"sex":@"male"}.mutableCopy;
        [mdic removeObjectForKey:@"age"];
        NSLog(@"%@",mdic);
        
# pragma mark ** 移除字典中所有的键值对 API
        [mdic removeAllObjects];
        NSLog(@"%@",mdic);
#endif
        /*创建一个可变字典 (26个键值对)*/
//        NSMutableDictionary *mdic = @{@"A":@[],
//                                 @"B":@[],
//                                 @"C":@[],
//                                 @"D":@[],
//                                 @"E":@[],
//                                 @"F":@[],
//                                 @"G":@[],
//                                 @"H":@[],
//                                 @"I":@[],
//                                 @"J":@[],
//                                 @"K":@[],
//                                 @"L":@[],
//                                 @"M":@[],
//                                 @"N":@[],
//                                 @"O":@[],
//                                 @"P":@[],
//                                 @"Q":@[],
//                                 @"R":@[],
//                                 @"S":@[],
//                                 @"T":@[],
//                                 @"U":@[],
//                                 @"V":@[],
//                                 @"W":@[],
//                                 @"X":@[],
//                                 @"Y":@[],
//                                 @"Z":@[]}.mutableCopy;
#if 0
        NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithCapacity:26];
        for (int i = 0; i < 26; i++) {
            /*创建key值*/
            
            NSString *key = [NSString stringWithFormat:@"%c",'A' + i];
            /*创建空的可变数组*/
            NSMutableArray *mArr = [NSMutableArray array];
            /*将字典中添加新的键值对*/
            [mdic setObject:mArr forKey:key];
            
        }
        
        
        
        
        
        /*创建联系人*/
        Contect *con1 = [[Contect alloc]initWithName:@"Zhangsan" phonenumber:@"123456"];
        Contect *con2 = [[Contect alloc]initWithName:@"Lisi" phonenumber:@"754545"];
        Contect *con3 = [[Contect alloc]initWithName:@"Goushi" phonenumber:@"5645321"];
        Contect *con4 = [[Contect alloc]initWithName:@"Dage" phonenumber:@"4252116"];
        Contect *con5 = [[Contect alloc]initWithName:@"Wang" phonenumber:@"5616163"];
        
        /*根据联系人姓名在字典中找出数组*/
        NSString *key = con1.group;
        NSMutableArray *mArr = [mdic objectForKey:key];
        /*将联系人对象添加到数组中*/
        [mArr addObject:con1];
        [[mdic objectForKey:con2.group] addObject:con2];
        [[mdic objectForKey:con3.group] addObject:con3];
        [[mdic objectForKey:con4.group] addObject:con4];
        [[mdic objectForKey:con5.group] addObject:con5];
        for (NSString *key in mdic) {
            NSArray *arr = [mdic objectForKey:key];
            for (Contect *contect in arr) {
                [Contect info];
            }
        }
        
        NSLog(@"%@",mdic);

        
#endif
# pragma mark 快速遍历容器(数组,字典,集合......)
        
#if 0
        NSArray *arr = @[@"zhangsan",@"lisi",@"wang"];
        
        /* 用forin  快速遍历数组 */
        for (NSString *str/*对象类型*/ in arr/*容器*/) {
            NSLog(@"%@",str);
        }
        
        Contect *con0 = [[Contect alloc]initWithName:@"Zhangsan" phonenumber:@"123456"];
        NSArray *conArr = @[con1];
        
        for (Contect *con in conArr) {
            [con info];
        }
        
#endif
        
        
#if 0
      /*forin快速遍历字典*/
        
        NSDictionary *dic = @{@"name":@"zhangsan",
                              @"age":@"20",
                              @"sex":@"male",
                              @"hobby":@"eat"};
        /* 对字典快速遍历  遍历的是key */
        for (NSString *key in dic) {
            NSLog(@"%@",[dic objectForKey:key]);
        }
        
        
        
        
#endif
# pragma mark - 知识点4 集合
# pragma mark * 不可变集合
# pragma mark ** 创建对象 API
#if 0
        /*注意  集合的互异性   不能存在相同的对象*/
        NSSet *set = [[NSSet alloc] initWithObjects:@"zhang",@"wang",@"li",@"zhang", nil];
        
        NSSet *set2 = [NSSet setWithObjects:@"zhang",@"wang",@"li",@"zhang", nil];
        
# pragma mark 个数
        NSLog(@"%ld",set.count);
# pragma mark 取出对象
        NSLog(@"%@",[set anyObject]);
# pragma mark 取出所有对象
        NSArray *arr = [set allObjects];
        NSLog(@"%@",arr);
        
# pragma mark 判断是否含有某一个对象
        if ([set containsObject:@"wu"]) {
            NSLog(@"含有");
        }else{
            NSLog(@"不含有");
        }
#endif
# pragma mark * 可变集合
# pragma mark ** 创建对象 API
        
        NSMutableSet *mSet = [[NSMutableSet alloc] initWithObjects:@"zhang",@"wang",@"li",@"li", nil];
        
        
        NSMutableSet *mSet2 = [NSMutableSet setWithObjects:@"zhang",@"wang",@"li",@"li", nil];
        
#pragma mark ** 添加对象
        [mSet addObject:@"li"];
#pragma mark ** 删除对象
        [mSet removeAllObjects];
        
#pragma mark   快速遍历集合
        for (NSString *str in mSet2) {
            NSLog(@"%@",str);
        }
        
        
        
        
        
        
    
    }
    return 0;
}





